#include <my_header.h>

/* Usage: ./2_anonymous_pipes */
int main(int argc, char *argv[]){                                  
    int pipefd[2];  // 数组存储管道
    pipe(pipefd);   // 读：pipefd[0] 写：pipefd[1]

    int pipe_fd = fork();
    ERROR_CHECK(pipe_fd, -1, "fork");

    if(pipe_fd == 0)
    {
        close(pipefd[1]);   // 只保留读
        char buf[5] = { 0 };
        read(pipefd[0], buf, sizeof(buf));

        printf("child get message:%s\n", buf);
    }else
    {
        close(pipefd[0]);   // 只保留写
        write(pipefd[1], "hello", 5);
        wait(NULL); // 阻塞 等待处理子进程的资源
    }
    return 0;
}


